<?php

class SF_Album_Block_Bestsales extends Mage_Catalog_Block_Product_Abstract
{
	
	protected $mainTitle = 'Best Sellers';
	protected $imgHeight = 234;
	protected $imgWidth = 234;

	protected function _toHtml() {		
        return parent::_toHtml();
    }
	
	public function getProductList() {

		//$categoryarr = array(4,537,5,586,726,792,484,394);
		$categoryarr = array(1066,1067,537,586,6,792,726,1068,394,484);
		
		$totalArr = array();
		foreach ($categoryarr as $value) {
			$root = Mage::getModel('catalog/category')->load($value); 
            $totalArr["categoryarrName"] = $root->getName();
            $totalArr["categoryarrUrl"]  = $root->getUrl();
			$totalArr["product"] = $this->getCollection($value);
			$totalArrs[$value] = $totalArr;
		}

		return $totalArrs;
    }

	public function getCollection($id){
		$pagesize = 15;
		$page = 1;
		$w = 234; 
		$h = 234; 
		$placeholderUrl  = $this->getMediaUrl('catalog/product/placeholder/default/placeholder-image-rumall.jpg');
		$coll = Mage::getModel('catalog/category')->load($id)
							->getProductCollection()
							->addAttributeToSelect('entity_id')
						    ->addAttributeToSelect('name')
							->addAttributeToSelect('pirce')
							->addAttributeToSelect('short_description')
						    ->addAttributeToSelect('thumbnail')
							->addUrlRewrite()
							->addFinalPrice()
							->addAttributeToFilter('status', 1)
							->addAttributeToFilter('visibility', 4)
							->addAttributeToFilter('approval', 2)
							->setPageSize(intval($pagesize))
							->setCurPage(intval($page));
		
		$coll->getSelect()->joinLeft(array("sfoi" => 'sales_flat_order_item'),
								"e.entity_id = sfoi.product_id",
								array('parent_item_id','qty_ordered'))
						 ->where("sfoi.parent_item_id is NUll")
					     ->columns(array('sfoi_qty_ordered' => 'sum(sfoi.qty_ordered)'))
						 ->group("e.entity_id")
					     ->order('sfoi_qty_ordered desc');
					     
		$products = array();
		foreach ($coll as $key => $_product) {

	        $finalPrice = $_product->getFinalPrice();
	        $imgUrl = (string)$this->helper('catalog/image')->init($_product, 'thumbnail')->resize($w,$h);
	        $productId = $_product->getId();
	        $products[$productId]['productUrl'] = $_product->getProductUrl();
	        $products[$productId]['productName'] = $_product->getName();
	        $products[$productId]['currencyPrice'] = Mage::helper('core')->currency($_product->getPrice(), true, false);
	        $products[$productId]['currencyFinalPrice'] =  Mage::helper('core')->currency($finalPrice, true, false);
	        $products[$productId]['shortdes'] = Mage::helper('core')->stripTags($_product->getShortDescription());
	        $products[$productId]['productimgUrl01'] = $imgUrl ? $imgUrl : $placeholderUrl;
	        $products[$productId]['productimgUrl02'] = $imgUrl ? $imgUrl : $placeholderUrl;
	        $products[$productId]['addToCart'] = Mage::helper('core')->quoteEscape($this->__('Add to Cart'));
	        $products[$productId]['addToWishlist'] = $this->helper('wishlist')->getAddUrl($_product);
			$products[$productId]['addToCompare'] = $this->getAddToCompareUrl($_product);
		}			
				
		return $products;	
	}

	public function getCategoryNameById($id){
		$category = Mage::getModel('catalog/category')->load($id);
		return $category->getName();
	}
	
	public function getMainTitle(){
		return $this->__($this->mainTitle);
	}


	public function getAddToCartUrl($product, $additional = array())
    {
        if ($this->hasCustomAddToCartUrl()) {
            return $this->getCustomAddToCartUrl();
        }

        if ($this->getRequest()->getParam('wishlist_next')) {
            $additional['wishlist_next'] = 1;
        }

        $addUrlKey = Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED;
        $addUrlValue = Mage::getUrl('*/*/*', array('_use_rewrite' => true, '_current' => true));
        $additional[$addUrlKey] = Mage::helper('core')->urlEncode($addUrlValue);

        return $this->helper('checkout/cart')->getAddUrl($product, $additional);
    }
    
   	public function getAddToCompareUrl($product)
    {
        return $this->helper('catalog/product_compare')->getAddUrl($product);
    }

    //you M版聚合页显示商品数据 2016-08-31
    public function  MobileCollection(){
		$pagesize = 16;
		$page = 1;
		$coll = Mage::getModel('catalog/category')
							->getProductCollection()
							->addAttributeToSelect('*')
							->addAttributeToFilter('status', 1)
							->addAttributeToFilter('visibility', 4)
							->addAttributeToFilter('approval', 2)
							->setPageSize(intval($pagesize))
							->setCurPage(intval($page));
		
		$coll->getSelect()->joinLeft(array("sfoi" => 'sales_flat_order_item'),
								"e.entity_id = sfoi.product_id",
								array())
						 ->joinLeft(array("sfo" => 'sales_flat_order'),
								"sfoi.order_id = sfo.entity_id",
								array())
						 ->where("sfoi.parent_item_id is NUll")
						 // ->where("sfo.status = 'payment_success' or sfo.status = 'pending'")
					     ->columns(array('sfoi_qty_ordered' => new Zend_Db_Expr('sum(sfoi.qty_ordered)')))
						 ->group("e.entity_id")
					     ->order('sfoi_qty_ordered desc');
					     
		return $coll;		
    }
}
